﻿
module.exports = {
    userList: {},

    //添加用户
    addUser: function (userId, socket) {

        var users = this.userList;

        //当前没有用户
        if (!users[userId]) {
            users[userId] = {
                userId: userId,
                socket: socket
            };
        } else {
            for (var uid in users) {
                if (uid == userId) {
                    users[uid].socket = socket;
                    break;
                }
            }
        }
    },

    //移除用户
    removeUser: function (socketId) {
        for (var user in this.userList) {
            if (this.userList[user].socket.id == socketId) {
                delete this.userList[user];
                break;
            }
        }
    },

    changeUser: function (userId, socket) {
        for (var id in this.userList) {
            var user = this.userList[id];
            if (user.userId == userId) {
                user.socket = socket;
                break;
            }
        }
    }
};